*------------------------------------------------------------------------------*
*																			   *
*					Get data ready for survival analysis				   	   *
*																			   *
*------------------------------------------------------------------------------*
																	
* Project: "Voting in Context" Book
* Title: "Always late? Stability and change in individuals‘ time of vote decisions"
* Author: Maria Preißinger 																																		


*-----------------------------------
* Long format
*------------------------------------

reshape long ///
	a_decided a_decidedm b_decided b_decidedm ///
	a_talk a_dtalk a_oeff a_paper a_countcontact ///
	b_talk b_dtalk b_oeff b_paper b_countcontact ///
	a_paneltalk a_paneloeff a_panelpaper a_panelcountcontact ///
	b_paneltalk b_paneloeff b_panelpaper b_panelcountcontact ///
	a_panelcongcontact a_panelcongtalk a_panelcongard a_panelcongzdf ///
	b_panelcongcontact b_panelcongtalk b_panelcongard b_panelcongzdf ///
	a_panelcongtvduell a_tvduell b_panelcongtvduell b_tvduell, ///
	i(newid0913) j(wave) 	
	



*-----------------------------------
* Time Variables
*--------------------------------------	
	
* Dummy for each wave
foreach num of numlist 1/7 {
gen wave`num' = 1 if wave==`num'  //Dummy for each wave
replace wave`num' = 0 if wave~=`num'
}

fre wave?

* Wave-Variable without "missing" waves
gen wavem = wave		//Wave variable for models (without wave 1 and 7)
replace wavem = . if wave==1 | wave==7

gen wavefd = wave //wave variable for fd-models (without wave 1, 2 and 7)
replace wavefd = . if wave==1 | wave==2 | wave==7

* Wave-Variable starting at zero
gen wave0 = wavem-2 //fängt an 0 an, gut zum Interagieren



*-------------------------------------------------------------------------
* Create dependent variable for intraindivdiual variation 
* in decision times between years
*----------------------------------------------------------------------------

lab define diff ///
	0 "both years undecided" ///
	1 "2009 decided, 2013 undecided" ///
	2 "2009 undecided, 2013 decided" ///
	3 "both years decided"

gen diff_decided_mlogit = 0 if a_decided==0 & b_decided==0
replace diff_decided_mlogit = 1 if a_decided==1 & b_decided==0
replace diff_decided_mlogit = 2 if a_decided==0 & b_decided==1
replace diff_decided_mlogit = 3 if a_decided==1 & b_decided==1

lab val diff_decided_mlogit diff 

fre diff_decided

* Missing after double failure (decided in both years)
foreach num of numlist 1/6 {
bysort newid0913: replace diff_decided_mlogit = . ///
	if diff_decided_mlogit[_n-`num']==3 
}

fre diff_decided  //641 individuals


list newid0913 wave a_decided b_decided diff_decided_mlogit if wkp0913==3, sepby(newid0913)
list newid0913 wave a_decided b_decided diff_decided_mlogit if wave==4 & a_decided==1 & b_decided==1, sepby(newid0913)



*------------------------------------------------------------------
* Create independent variables for analysis comparing between years
*-----------------------------------------------------------------------

foreach var in ///
	paneltalk paneloeff panelpaper tvduell panelcountcontact  ///
	panelcongcontact panelcongtalk panelcongard panelcongzdf ///
	panelcongtvduell  {
gen diff_`var' = ///
	b_`var' - a_`var'
}
		
list newid0913 wave b_panelcongtalk a_panelcongtalk diff_panelcongtalk ///
	if wkp0913==3, sepby(newid0913)
		
	
*----------------------------------------------------------------------
* Get rid of observations after final decision for single-year models
*-----------------------------------------------------------------------

replace a_decided = . if wave>a_timedeci2b
replace a_decidedm = . if wave>a_timedeci1b

replace b_decided = . if wave>b_timedeci2b
replace b_decidedm = . if wave>b_timedeci1b

fre ?_decided*  //=1 are now also the number of individuals

list newid0913 wave a_decided if a_timedeci2b~=., sepby(newid0913) 
list newid0913 wave b_decided if b_timedeci2b~=., sepby(newid0913) 

	
*-----------------------------------------------------------------------------
* Compute First Differences of time varying covariates for single year models
*------------------------------------------------------------------------------

foreach var of varlist ///
	?_talk ?_countcontact ?_paper ?_oeff ?_tvduell ///
	?_panelcong*  {
	
bysort newid0913: gen fd_`var' = `var' - `var'[_n-1]
	
}
	

save "always late all modifications long format", replace
